package com.china08.yxyapi.api.manage.sch.office.cookbook;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.cookbook.CookBook;
import com.china08.yxyapi.entity.mg.cookbook.CookBook.Meal;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.cookbook.YxySchManageCookbookRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateUtils;

/**
 * 管理员每周食谱
 * 
 */
@CrossOrigin
@RestController
public class YxySchManagerCookbookCrtController extends YxyApiBaseController {

    @Autowired
    private YxySchManageCookbookRepository yxySchManageCookbookRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/cookbook/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model) {
        CookBook ck = new CookBook();
        YxyProfile profile = getProfile(request, response);

        ck.setId(profile.getSchoolId() + model.weekId);
        ck.setSchoolId(profile.getSchoolId());
        ck.setSchoolName(profile.getSchoolName());
        ck.setWeekId(model.weekId);
        ck.setWeekName(DateUtils.Week1(model.weekId));

        // 早餐
        Meal meal = new CookBook().new Meal();
        meal.setImages(model.breakfast.images);
        meal.setName(model.breakfast.name);
        ck.setBreakfast(meal);
        // 早点
        Meal meal2 = new CookBook().new Meal();
        meal2.setImages(model.breakfastCakes.images);
        meal2.setName(model.breakfastCakes.name);
        ck.setBreakfastCakes(meal2);
        // 午餐
        Meal meal3 = new CookBook().new Meal();
        meal3.setImages(model.lunch.images);
        meal3.setName(model.lunch.name);
        ck.setLunch(meal3);
        // 午餐点心
        Meal meal4 = new CookBook().new Meal();
        meal4.setImages(model.lunchCakes.images);
        meal4.setName(model.lunchCakes.name);
        ck.setLunchCakes(meal4);
        // 晚餐
        Meal meal5 = new CookBook().new Meal();
        meal5.setImages(model.dinner.images);
        meal5.setName(model.dinner.name);
        ck.setDinner(meal5);
        yxySchManageCookbookRepository.save(ck);
        return ck;
    }

    /**
     * 请求参数 weekId（周日为1,周一为2,以此类推）
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 日期
        public Integer weekId;
        // 早餐数据
        public Meals breakfast;
        // 早点数据
        public Meals breakfastCakes;
        // 午餐数据
        public Meals lunch;
        // 下餐点心数据
        public Meals lunchCakes;
        // 晚餐数据
        public Meals dinner;
    }

    public static class Meals {
        // 食谱名称
        public String name;
        // 对应的图片链接地址
        public List<String> images;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {

    }

}
